
'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE,BOOLEAN } = app.Sequelize;

  const sys_role = app.model.define('sys_role', {
    roleId: {
      type: INTEGER(20),
      comment: "角色ID",
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field:"role_id"
    },
    roleName:{
      type:STRING(30),
      comment:"角色名称",
      allowNull:false,
      field:"role_name"
    },
    roleKey:{
      type:STRING(100),
      comment:"角色权限字符串",
      allowNull:false,
      field:"role_key"
    },
    roleSort:{
      type:INTEGER(4),
      comment:"显示顺序",
      allowNull:false,
      field:"role_sort"
    },
    dataScope:{
      type:STRING(1),
      comment:"数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限 5：仅本人数据权限）",
      defaultValue:"1",
      field:"data_scope"
    },
    menu_check_strictly:{
      type:INTEGER(1),
      comment:"菜单树选择项是否关联显示",
      defaultValue:1,
      field:"menu_check_strictly",
      
    },
    menuCheckStrictly:{
      type:BOOLEAN,
      get(){
        return this.menu_check_strictly === 1 ? true : false;
      }
    },
    dept_check_strictly:{
      type:INTEGER(1),
      comment:"部门树选择项是否关联显示",
      defaultValue:1
    },
    deptCheckStrictly:{
      type:BOOLEAN,
      get(){
        return this.dept_check_strictly === 1 ? true : false;
      }
    },
    status:{
      type:STRING(1),
      comment:"角色状态（0正常 1停用）",
      allowNull:false
    },
    createBy:{
      type:STRING(64),
      comment:"创建者",
      defaultValue:"",
      field:"create_by"
    },
    updateBy:{
      type:STRING(64),
      comment:"更新者",
      defaultValue:"",
      field:"update_by"
    },
    remark:{
      type:STRING(500),
      comment:"备注",
      defaultValue:""
    },
    createTime:{
      type:DATE,
      field:"created_at"
    }
  },{
    paranoid:true,
    freezeTableName:true
  });

  sys_role.associate = ()=>{
    app.model.SysRole.belongsToMany(app.model.SysUser,{
      through:app.model.SysUserRole
    });
    app.model.SysRole.belongsToMany(app.model.SysMenu, {
      through: app.model.SysRoleMenu,
      allowNull:true
    });
    app.model.SysRole.belongsToMany(app.model.SysDept,{
      through:app.model.SysRoleDept
    });
  };


  return sys_role;
};